// pages/user/user.js
import { Order } from './delivery-model.js'
const app = getApp()
const order = new Order()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        wxUid: '',
        storeNo: '',
        leftBar: '22',
        nowId: 1,
        orderList: [],
        hasMore: false,
        page: 1,
        noneData: false,
        threeOrderCount: null,
        allowClick: true,
        soldoutList: null,
        loadDone:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        
        wx.showLoading({
            title: '加载中',
        })
        this.setData({
            storeNo: wx.getStorageSync('storeNo'),
            wxUid: wx.getStorageSync('wxUid'),
        })
        this.commonRequest('S', 'LW', '')
    },
    //   公共的请求
    commonRequest(orderStatus, logisticStatus, pulldown) {
        let { page, userInfo, orderList, wxUid } = this.data
        let params = {
            "data": {
                wxUid,
                "orderStatus": orderStatus,
                "logisticStatus": logisticStatus,
                "page": '' + page
            }
        }
        this.commonGetOrderCount()
        order.getOrder(params, res => {
            wx.hideLoading()
            wx.stopPullDownRefresh()
            if (res.status) {
                res.data.forEach(val => {
                    val.update_time = order.formate(val.update_time)
                    let nowPrice
                    if (val.couponAmount) {
                        nowPrice = ((Number(val.price) + Number(val.postage)) * 10 - (Number(val.couponAmount)) * 10) / 10
                    } else {
                        nowPrice = Number(val.price) + Number(val.postage)
                    }
                    val.price = nowPrice
                })
                if (res.total > orderList.length && pulldown !== 'pulldown') {

                    orderList = [...orderList, ...res.data]
                    console.log('orderList', orderList)

                    this.setData({
                        orderList,
                        hasMore: true,
                        loadDone: true
                    })
                } else if (pulldown == 'pulldown') {
                    orderList = res.data
                    console.log('orderList-pull', orderList)
                    this.setData({
                        orderList,
                        hasMore: true,
                        loadDone: true
                    })
                } else {
                    this.setData({
                        hasMore: false,
                        loadDone: true
                    })
                }
                if (res.total == orderList.length) {
                    this.setData({
                        hasMore: false,
                        loadDone: true
                    })
                }
                if (res.total == 0) {
                    this.setData({
                        noneData: true,
                        hasMore: false,
                        loadDone: true
                    })
                } 
            } else {
               
                wx.showToast({
                    title: '服务器错误',
                    icon: 'none'
                })
            }
        })
    },
    //   跳转详情
    jumpDetail(e) {
        let { storeNo } = this.data;
        let id = order.getDataSet(e, "id");
        wx.navigateTo({
            url: '/pages/orderDetail/orderDetail?orderId=' + id + '&storeNo=' + storeNo,
        })
    
    },
    jumpGoods(e) {
        let { storeNo } = this.data;
        let id = order.getDataSet(e, "id");
        wx.navigateTo({
            url: '/pages/product/product?id=' + id + '&storeNo=' + storeNo,
        })
    },
    //   删除订单
    deleteOrder(e) {
        var id = e.currentTarget.dataset.id
        let that = this
        wx.showModal({
            title: '提示',
            content: '确认删除该订单吗？',
            success: function (res) {
                if (res.confirm) {
                    console.log('用户点击确定')
                    let params = {
                        "data": {
                            "orderId": "" + id
                        }
                    }
                    wx.showLoading({
                        title: '加载中',
                    })
                    order.deleteOrder(params, res => {
                        if (res.status) {
                            wx.hideLoading()
                            wx.showToast({
                                title: '删除成功',
                                icon: 'success',
                                duration: 2000,
                                success() {
                                    setTimeout(() => {
                                        that.onPullDownRefresh()
                                    }, 1500)
                                }
                            })
                        }
                    })

                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    trace(e) {
        var id = e.currentTarget.dataset.id
        var addressId = e.currentTarget.dataset.address
        wx.navigateTo({
            url: '/pages/orderTrace/orderTrace?addressId=' + addressId + '&orderId=' + id
        })
    },
    //退款
    wxRefund(e) {
        let that = this
        var dataset = e.currentTarget.dataset
        var id = dataset.id
        let { orderList, allowClick} = this.data
        let params = {
            data: {
                "orderId": id,
                "orderStatus": "AU"
            }
        }
        this.setData({
            allowClick: false
        })
       
        wx.showModal({
            title: '提示',
            content: '确定退款吗？',
            success(res){
                if(res.confirm){
                    wx.showLoading({
                        title: '退款中',
                    })
                    order.wxRefund(params, res => {
                        wx.hideLoading()
                        if (res.status) {
                            console.log(res)
                            wx.showToast({
                                title: '退款申请已提交',
                                icon: 'success',
                                duration: 2000,
                                success() {
                                    setTimeout(() => {
                                        that.onPullDownRefresh()
                                    }, 2000)
                                    that.setData({
                                        allowClick: true
                                    })
                                }
                            })
                            // let data = res.data
                            // if (data.result_code == 'SUCCESS' && data.return_code == 'SUCCESS'){
                            // }else{
                            //     order.showMyTip({ _this: that, title: '退款失败' })                                
                            // }
                        } else {
                            that.setData({
                                allowClick: true
                            })
                            wx.hideLoading()
                            wx.showToast({
                                title: '服务器错误',
                                icon: 'none'
                            })
                        }
                    })

                }else{
                    that.setData({
                        allowClick: true
                    })
                }
            }
        })
        
    },
    soldoutBack() {
        this.setData({
            soldoutList: null,
            allowClick: true

        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var { page, hasMore,loadDone } = this.data;
        /**
         * 此处加上数据大小的判断
         */
        
        if (loadDone){
            wx.pageScrollTo({
                scrollTop: 0,
                duration: 0
            })
            this.setData({
                page: 1,
                allowClick: true
            })
            this.commonRequest('S', 'LW', 'pulldown')
            
        }
        
    },
    showAddAddress() {
        this.setData({
            dialogShow: true
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        var { nowId } = this.data;
        this.setData({
            page: 1,
        })
        this.commonRequest('S', 'LW', 'pulldown')
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        var { page, hasMore } = this.data;
        /**
         * 此处加上数据大小的判断
         */
        if (hasMore) {
            this.setData({
                page: page + 1
            })
            this.commonRequest('S', 'LW', '')
        }
    },

    commonGetOrderCount() {
        let parmas = {
            "data": {
                wxUid: this.data.wxUid
            }
        }
        order.getOrderCount(parmas, res => {
            if (res.status) {
                this.setData({
                    threeOrderCount: res.data
                })

            } else {
                wx.hideLoading()
                wx.showToast({
                    title: '服务器错误',
                    icon: 'none'
                })
                wx.stopPullDownRefresh()
            }
        })
    },
   
})